package com.ys.nms.module.business.controller.admin.ipv4Addresses;

import com.baomidou.dynamic.datasource.annotation.Slave;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;
import static com.ys.nms.framework.common.pojo.PageParam.PAGE_SIZE_NONE;

import com.ys.nms.module.business.controller.admin.ipv4Addresses.vo.*;
import com.ys.nms.module.business.dal.dataobject.ipv4Addresses.Ipv4AddressesDO;
import com.ys.nms.module.business.service.ipv4Addresses.Ipv4AddressesService;

@Tag(name = "管理后台 - IP地址")
@RestController
@RequestMapping("/business/ipv4-addresses")
@Validated
public class Ipv4AddressesController {

    @Resource
    private Ipv4AddressesService ipv4AddressesService;

    @PostMapping("/create")
    @Operation(summary = "创建IP地址")
    public CommonResult<Integer> createIpv4Addresses(@Valid @RequestBody Ipv4AddressesSaveReqVO createReqVO) {
        return success(ipv4AddressesService.createIpv4Addresses(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IP地址")
    public CommonResult<Boolean> updateIpv4Addresses(@Valid @RequestBody Ipv4AddressesSaveReqVO updateReqVO) {
        ipv4AddressesService.updateIpv4Addresses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IP地址")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteIpv4Addresses(@RequestParam("id") Integer id) {
        ipv4AddressesService.deleteIpv4Addresses(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除IP地址")
    public CommonResult<Boolean> deleteIpv4AddressesList(@RequestParam("ids") List<Integer> ids) {
        ipv4AddressesService.deleteIpv4AddressesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IP地址")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Ipv4AddressesRespVO> getIpv4Addresses(@RequestParam("id") Integer id) {
        Ipv4AddressesDO ipv4Addresses = ipv4AddressesService.getIpv4Addresses(id);
        return success(BeanUtils.toBean(ipv4Addresses, Ipv4AddressesRespVO.class));
    }


    @Slave
    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得IP地址分页")
    public CommonResult<PageResult<Ipv4AddressesRespVO>> getIpv4AddressesPage(@Valid Ipv4AddressesPageReqVO pageReqVO) {
        PageResult<Ipv4AddressesDO> pageResult = ipv4AddressesService.getIpv4AddressesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Ipv4AddressesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IP地址 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIpv4AddressesExcel(@Valid Ipv4AddressesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PAGE_SIZE_NONE);
        List<Ipv4AddressesDO> list = ipv4AddressesService.getIpv4AddressesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "IP地址.xls", "数据", Ipv4AddressesRespVO.class,
                        BeanUtils.toBean(list, Ipv4AddressesRespVO.class));
    }

}